package cn.jdy.plugins.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Notice;

/**
 * @author yym
 * @title : 通知通报操作实现
 */
@Service
public class NoticeService extends BaseDao<Notice> {

	/**
	 * 按条件查询
	 * @param Notice
	 * @return
	 */
	public List<Notice> query(Notice notice, String... date) {
		if(notice == null) return super.findAll();
		//return super.find(createFilter(Notice, Notice.getiDisplayLength() > 0, date));
		if(notice.getiDisplayLength() > 0){
		  return super.find(createFilter(notice, date),notice.getiDisplayStart(),notice.getiDisplayLength());
		}
		return super.find(createFilter(notice, date));
	}

	public Long count(Notice notice, String... date) {
		return super.count(createFilter(notice, date));
	}
	
	/**
	 * 拼装条件
	 * @param Notice
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(Notice notice, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  if(!StringUtil.isNullOrEmpty(notice.getKeywords())) {
      if(!StringUtil.isNullOrEmpty(notice.getKeyType())) {
        dc.add(Restrictions.like(notice.getKeyType(), notice.getKeywords().trim(),MatchMode.ANYWHERE));
      }else
        //TODO 添加模糊搜索
        ;
    }
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("falseCreateTime", date[0],date[1]));
	  }
//	  if(!StringUtil.isNullOrEmpty(notice.getStatus())){
//	    dc.add(Restrictions.eq("status", notice.getStatus()));
//	  }
	  if(!StringUtil.isNullOrEmpty(notice.getUserid())){
      dc.add(Restrictions.eq("userid", notice.getUserid()));
    }
	  if(!StringUtil.isNullOrEmpty(notice.getSortType())) {
	    dc.addOrder(Order.desc("sortType"));
	  }
	  dc.add(Restrictions.or(Restrictions.eq("status", "5"),Restrictions.eq("status", "6"),Restrictions.eq("status", "7")));
	  dc.addOrder(Order.desc("topTime"));
	  dc.addOrder(Order.desc("falseCreateTime"));
		return dc;
	}

	/**
	 * 查询热门新闻
	 * @return
	 */
	public List<Notice> queryHotNotice() {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
//		dc.add(Restrictions.eq("status", "1"));//前台查询需展示有效状态文章
    dc.addOrder(Order.desc("readTimes"));
    return super.find(dc,0,5);
		
	}
	
	/**
	 * @since 040116
	 * 模糊查询新闻
	 * @return
	 * 加入columnId
	 */
	public List<Notice> queryByLike(Notice notice,String keywords) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.add(Restrictions.eq("status", notice.getStatus()));//前台查询需展示有效状态文章
	  dc.add(Restrictions.or(
	      Restrictions.like("title", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("keyword", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("summary", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("context", keywords, MatchMode.ANYWHERE)
	  ));
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc,notice.getiDisplayStart(),notice.getiDisplayLength());
	}
	
	/**
	 * 模糊查询新闻
	 * @return
	 */
	public int queryByLikeCount(Notice notice,String keywords) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
//	  dc.add(Restrictions.eq("status", notice.getStatus()));//前台查询需展示有效状态文章
	  dc.add(Restrictions.or(
	      Restrictions.like("title", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("keyword", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("summary", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("context", keywords, MatchMode.ANYWHERE)
	  ));
	  dc.addOrder(Order.desc("topTime"));
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc).size();
	}

	/**
	 * 按照类型查询 文章
	 * @return
	 */
	public List<Notice> findbytype(Long columnid,int firstResult, int maxResults, boolean isImg) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
//	  dc.add(Restrictions.eq("status", "1"));//前台查询需展示有效状态文章
	  if(null != columnid){
	    dc.add(Restrictions.eq("columnId", columnid));
	  }
	  if(isImg){// 是否有封面图片
	    dc.add(Restrictions.ne("imgUrl", ""));
	  }
	  dc.addOrder(Order.desc("topTime"));
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc, firstResult, maxResults);
	}
	
	/**
	 * 按照大分类 查询 文章
	 * @return
	 */
	public List<Notice> findbyPid(Long pid,int firstResult, int maxResults, boolean isImg) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.createAlias("cv", "cv");
//	  dc.add(Restrictions.eq("status", "1"));//前台查询需展示有效状态文章
	  if(isImg){// 是否有封面图片
	    dc.add(Restrictions.ne("imgUrl", ""));
    }
	  if(null != pid){
	    dc.add(Restrictions.eq("cv.pid", pid));
	  }
	  dc.addOrder(Order.desc("topTime"));
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc,firstResult,maxResults);
	}
	
	/**
	 * 站点地图
	 * @return
	 */
	public List<Map<String, String>> querySiteMap(int start,int limit) {
	  String sql = "select id,title,createTime from "+entityClass.getSimpleName()+" where status =1 limit ?,?";
    SQLQuery sqlQuery = super.em().createSQLQuery(sql);
    sqlQuery.setParameter(0, start);
    sqlQuery.setParameter(1, limit);
    // 将查询寻结果转换为Map格式，解决多表查询或零散字段查询 映射取值问题
    sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    return sqlQuery.list();
	}
	
	
	/**
	 * 特警
	 * 按条件查询
	 * @param Notice
	 * @return
	 */
	public List<Notice> queryt(Notice notice, String... date) {
		if(notice == null) return super.findAll();
		//return super.find(createFilter(Notice, Notice.getiDisplayLength() > 0, date));
		if(notice.getiDisplayLength() > 0){
		  return super.find(createFiltert(notice, date),notice.getiDisplayStart(),notice.getiDisplayLength());
		}
		return super.find(createFiltert(notice, date));
	}

	public Long countt(Notice notice, String... date) {
		return super.count(createFiltert(notice, date));
	}
	
	/**
	 * 拼装条件
	 * @param Notice
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFiltert(Notice notice, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  if(!StringUtil.isNullOrEmpty(notice.getKeywords())) {
      if(!StringUtil.isNullOrEmpty(notice.getKeyType())) {
        dc.add(Restrictions.like(notice.getKeyType(), notice.getKeywords().trim(),MatchMode.ANYWHERE));
      }else
        //TODO 添加模糊搜索
        ;
    }
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("createTime", date[0],date[1]));
	  }
	  if(!StringUtil.isNullOrEmpty(notice.getStatus())){
	    dc.add(Restrictions.eq("status", notice.getStatus()));
	  }
	  if(!StringUtil.isNullOrEmpty(notice.getUserid())){
      dc.add(Restrictions.eq("userid", notice.getUserid()));
    }
	  if(!StringUtil.isNullOrEmpty(notice.getSortType())) {
	    dc.addOrder(Order.desc("sortType"));
	  }
	  dc.addOrder(Order.desc("createTime"));
		return dc;
	}

	/**
	 * 按条件查询
	 * @param Notice
	 * @return
	 */
	public List<Notice> queryq(Notice notice, String... date) {
		if(notice == null) return super.findAll();
		//return super.find(createFilter(Notice, Notice.getiDisplayLength() > 0, date));
		if(notice.getiDisplayLength() > 0){
		  return super.find(createFilterq(notice, date),notice.getiDisplayStart(),notice.getiDisplayLength());
		}
		return super.find(createFilterq(notice, date));
	}

	public Long countq(Notice notice, String... date) {
		return super.count(createFilterq(notice, date));
	}
	
	/**
	 * 拼装条件
	 * @param Notice
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilterq(Notice notice, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  if(!StringUtil.isNullOrEmpty(notice.getKeywords())) {
      if(!StringUtil.isNullOrEmpty(notice.getKeyType())) {
        dc.add(Restrictions.like(notice.getKeyType(), notice.getKeywords().trim(),MatchMode.ANYWHERE));
      }else
        //TODO 添加模糊搜索
        ;
    }
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("falseCreateTime", date[0],date[1]));
	  }
	  if(!StringUtil.isNullOrEmpty(notice.getStatus())){
	    dc.add(Restrictions.eq("status", notice.getStatus()));
	  }
	  if(!StringUtil.isNullOrEmpty(notice.getUserid())){
      dc.add(Restrictions.eq("userid", notice.getUserid()));
    }
	  if(!StringUtil.isNullOrEmpty(notice.getSortType())) {
	    dc.addOrder(Order.desc("sortType"));
	  }
	  dc.addOrder(Order.desc("topTime"));
	  dc.addOrder(Order.desc("falseCreateTime"));
		return dc;
	}

}
